﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SIMc
{
    public class TabPageController { 
        XMLContactList contactlist;
        XMLHistory xmlHistory;
        TabControl tabControl;
        public Profile profil;
        public bool keybEnabled;
        public int keybHeight;
        //
        public TabPageController(XMLContactList kontaktList, TabControl tabControl, Profile profil, XMLHistory xmlHistory){
            this.contactlist= kontaktList;
            this.tabControl = tabControl;
            this.profil = profil;
            this.xmlHistory = xmlHistory;
        }

        public MyTabPage addTabPage(string name, string uri){
            MyTabPage newTabPage = new MyTabPage();
            if (keybEnabled) {
                newTabPage.P_chat.Height = newTabPage.P_chat.Height - keybHeight;
            }
            //prirobit hodenie focusu do pisacieho pola
            //newTabPage.GotFocus
            newTabPage.B_close.Click += new EventHandler(this.closeTab);
            newTabPage.B_send.Click += new EventHandler(this.sendMessage);
            newTabPage.Text = name;
            newTabPage.Tag = uri;
            this.tabControl.Controls.Add(newTabPage);
            return newTabPage;
        }

        // upravi adresu from (odstrani < >  a sip)
        public string getUriFromString(string odstran)
        {
            odstran = odstran.Remove(0, 1);
            odstran = odstran.Remove(odstran.Length - 1, 1);
            return odstran;
        }
        //TODO mrkne ci ma v contact liste
        //      ano -> priradi display name
        //      nie -> necha adresu
        public string chooseName(string from) {
            if (contactlist.isInContactList(profil.ID,from))
            { //je v kontakt liste
                Contact kontaktik = contactlist.getContactFromUri(profil.ID, from);
                return kontaktik.displayName;
            }
            else { //nie je v kontakt liste, hodim len adresu
                return from;
            }

        }

        //TODO mrkne ci tab s tymto menom existuje
        //      ano -> prida text
        //      nie -> prida tab aj text
        public void insertIntoTab(string from, string text){
            from = getUriFromString(from);
            string tab = chooseName(from);
            foreach(TabPage page in tabControl.TabPages){
                if (page.Text.Equals(tab)){
                        foreach (Control kontrol in tabControl.Controls)
                        {
                            if (kontrol.Equals(page))
                            {
                                MyTabPage moja = (MyTabPage)kontrol;
                                DateTime cas = DateTime.Now;
                                xmlHistory.addPost(profil.ID, (string)moja.Tag, tab + " [ " + DateTime.Now.ToString("h:MM tt") + " ]", text);
                                moja.TB_chat.Text = moja.TB_chat.Text + tab +" [ "+ DateTime.Now.ToString("h:MM tt")+" ]" + "\r\n";
                                moja.TB_chat.Text = moja.TB_chat.Text + text + "\r\n";
                                moja.TB_chat.SelectionStart = moja.TB_chat.Text.Length;
                                moja.TB_chat.ScrollToCaret();
                                return;
                            }
                        }
                }
            }
            
            MyTabPage newPage = addTabPage(tab, from);
            xmlHistory.addPost(profil.ID, (string)newPage.Tag, tab + " [ " + DateTime.Now.ToString("h:MM tt") + " ]", text);
            newPage.TB_chat.Text = newPage.TB_chat.Text + tab + " [ " + DateTime.Now.ToString("h:MM tt") + " ]" + "\r\n";
            newPage.TB_chat.Text = newPage.TB_chat.Text + text + "\r\n";
            newPage.TB_chat.SelectionStart = newPage.TB_chat.Text.Length;
            newPage.TB_chat.ScrollToCaret();
            return;
        }

        /// <summary>
        /// chcem poslat spravu niekomu z contactlistu
        /// </summary>
        /// <param name="name"></param>
        public void wantToWrite(string name, string uri) {
            string tab = name;
            int i=1;
            foreach (TabPage page in tabControl.TabPages)
            {
                if (page.Text.Equals(tab))
                {
                    tabControl.SelectedIndex=i;
                    return;
                }
                i++;
            }

            MyTabPage newPage = addTabPage(tab, uri);
            //newPage.B_send.Click+= new EventHandler(this.sendMessage);
            tabControl.SelectedIndex = i;
            return;
        }

        /// <summary>
        /// klik na poslanie spravy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sendMessage(object sender, EventArgs e) {
            //MessageBox.Show(((Button)sender).Parent.Parent.Text);
            MyTabPage tenTabPage = ((MyTabPage)(((Button)sender).Parent.Parent));

            xmlHistory.addPost(profil.ID, (string)tenTabPage.Tag, "I >>"+" [ " + DateTime.Now.ToString("h:MM tt") + " ]", tenTabPage.TF_send.Text);

            tenTabPage.TB_chat.Text += "I >>" + " [ " + DateTime.Now.ToString("h:MM tt") + " ]" + "\r\n";
            //tenTabPage.TB_chat.Text += "JA" + "\r\n";
            tenTabPage.TB_chat.Text += tenTabPage.TF_send.Text + "\r\n";
            tenTabPage.TB_chat.SelectionStart = tenTabPage.TB_chat.Text.Length;
            tenTabPage.TB_chat.ScrollToCaret();
            tenTabPage.TF_send.Focus();
            ((Chat)tenTabPage.Parent.Parent.Parent).posliSpravu((string)tenTabPage.Tag, tenTabPage.TF_send.Text);
            tenTabPage.TF_send.Text = "";
        }

        /// <summary>
        /// klik na zavretie tabu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeTab(object sender, EventArgs e)
        {
            //MessageBox.Show(((Button)sender).Parent.Parent.Text);
            MyTabPage tenTabPage = ((MyTabPage)(((Button)sender).Parent.Parent));
            tenTabPage.Dispose();
            if (tabControl.TabPages.Count == 0)
            {
                tabControl.Parent.Parent.Hide();
                ((Chat)tabControl.Parent.Parent).closed = true;
            }
        }

        internal void inputPanelEnabled(int oKolko)
        {
            keybEnabled = true;
            foreach (TabPage page in tabControl.TabPages)
            {
                    foreach (Control kontrol in tabControl.Controls)
                    {
                        if (kontrol.Equals(page))
                        {
                            MyTabPage moja = (MyTabPage)kontrol;
                            moja.P_chat.Height = moja.P_chat.Height-oKolko;
                            //return;
                        }
                    }
                }
        }

        internal void inputPanelDisabled(int oKolko)
        {
            keybEnabled = false;
            foreach (TabPage page in tabControl.TabPages)
            {
                foreach (Control kontrol in tabControl.Controls)
                {
                    if (kontrol.Equals(page))
                    {
                        MyTabPage moja = (MyTabPage)kontrol;
                        moja.P_chat.Height = moja.P_chat.Height + oKolko;
                        //return;
                    }
                }
            }
        }


    }

    public class MyTabPage : TabPage
    {
        public System.Windows.Forms.TextBox TF_send;
        private System.Windows.Forms.Panel panel2;
        public System.Windows.Forms.TextBox TB_chat;
        public System.Windows.Forms.Button B_send;
        public System.Windows.Forms.Button B_close;
        private System.Windows.Forms.Panel P_close;
        public System.Windows.Forms.Panel P_chat;

        public MyTabPage() {

            this.B_close = new Button();
            this.TB_chat = new System.Windows.Forms.TextBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.P_chat = new System.Windows.Forms.Panel();
            this.P_close = new System.Windows.Forms.Panel();
            this.TF_send = new System.Windows.Forms.TextBox();
            this.B_send = new System.Windows.Forms.Button();
            // 
            // tabPage1
            // 
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.P_close);
            this.Controls.Add(this.P_chat);
            this.AutoScroll = true;
            this.Location = new System.Drawing.Point(0, 0);
            this.Name = "MyTabPage";
            this.Size = new System.Drawing.Size(237, 236);
            this.Text = "MyTabPage";
            // 
            // TB_chat
            // 
            this.TB_chat.AcceptsReturn = true;
            this.TB_chat.AcceptsTab = true;
            this.TB_chat.Dock = System.Windows.Forms.DockStyle.Fill;
            this.TB_chat.Location = new System.Drawing.Point(0, 0);
            this.TB_chat.Multiline = true;
            this.TB_chat.Name = "TB_chat";
            this.TB_chat.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.TB_chat.Size = new System.Drawing.Size(237, 180);
            this.TB_chat.TabIndex = 2;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.TF_send);
            this.panel2.Controls.Add(this.B_send);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 212);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(237, 21);
            // 
            // P_close
            // 
            this.P_close.Controls.Add(this.B_close);
            this.P_close.Dock = System.Windows.Forms.DockStyle.Top;
            this.P_close.Location = new System.Drawing.Point(0, 0);
            this.P_close.Name = "P_close";
            this.P_close.Size = new System.Drawing.Size(237, 21);
            // 
            // P_chat
            // 
            this.P_chat.Controls.Add(this.TB_chat);
            this.P_chat.Dock = System.Windows.Forms.DockStyle.None;
            this.P_chat.Location = new System.Drawing.Point(0, 22);
            this.P_chat.Name = "P_chat";
            this.P_chat.Size = new System.Drawing.Size(237, 193);
            // 
            // TF_send
            // 
            this.TF_send.Dock = System.Windows.Forms.DockStyle.Left;
            this.TF_send.Location = new System.Drawing.Point(0, 0);
            this.TF_send.Name = "TF_send";
            this.TF_send.Size = new System.Drawing.Size(187, 21);
            this.TF_send.TabIndex = 1;
            // 
            // B_close
            // 
            this.B_close.Dock = System.Windows.Forms.DockStyle.Top;
            this.B_close.Location = new System.Drawing.Point(0, 0);
            this.B_close.Name = "B_close";
            this.B_close.Size = new System.Drawing.Size(237, 21);
            this.B_close.TabIndex = 2;
            this.B_close.Text = "Close tab";
            
            // 
            // B_send
            // 
            this.B_send.Dock = System.Windows.Forms.DockStyle.Right;
            this.B_send.Location = new System.Drawing.Point(180, 0);
            this.B_send.Name = "B_send";
            this.B_send.Size = new System.Drawing.Size(51, 21);
            this.B_send.TabIndex = 2;
            this.B_send.Text = "Send";
            //this.B_send.Click += new System.EventHandler(this.B_send_Click);
        }
    }
}
